﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdsDrawProfile.h"

#include "GmUiStructureCommandRegister.h"

#include "ActionDrawStraightElementInput.h"
#include "ActionDrawStraightElementCreator.h"
#include "ActionDrawArcBy3PtsElementInput.h"
#include "ActionDrawArcBy3PtsElementCreator.h"
#include "Vector2dUtils.h"

#include "IModelView.h"
#include "IUiView.h"

#include "IUserTransaction.h"
#include "IModelLine.h"
#include "ILine3d.h"
#include "IArc3d.h"

#include "ProfileUsedModelLineUtils.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

REGISTER_GM_COMMAND(CmdDrawStraightProfile)
REGISTER_GM_COMMAND(CmdDrawArcProfile)

OwnerPtr<IAction> CmdDrawStraightProfile::ExecuteCommand(const CommandParameters& cmdParams)
{
    IUiDocument* pUIDoc = UiDocumentViewUtils::GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUIDoc, L"pUIDoc指针无效",L"GDMPLab",L"2024-03-30");

    CmdDrawProfileBase* pCmdProfileBase = static_cast<CmdDrawProfileBase*>(this);
    ActionDrawStraightElementInput actionInput;
    actionInput.CommandId = GetCommandId();
    actionInput.DrawLineMode = DrawLineMode::Chained;
    actionInput.LineFinishedCallback = ActionDrawStraightElementInput::CreateOnLineFinishedCallback(&CmdDrawStraightProfile::OnLineFinished, this);
    actionInput.EnterGettingEndPointStateCallback = ActionDrawStraightElementInput::CreateOnEnterGettingEndPointStateCallback(
        &CmdDrawProfileBase::OnEnterGettingEndPointState, pCmdProfileBase);
    actionInput.ActionDestroyCallback = ActionDrawStraightElementInput::CreateOnActionDestroyCallback(&CmdDrawProfileBase::OnActionDestroy, pCmdProfileBase);
    actionInput.PropertyPanelEventCallback = ActionDrawStraightElementInput::CreateOnPropertyPanelEventCallback(&CmdDrawProfileBase::OnPropertyPanelEvent, pCmdProfileBase);

    OwnerPtr<IAction> opAction = ActionDrawStraightElementCreator::Create(actionInput);

    return TransferOwnership(opAction);
}

ElementId CmdDrawStraightProfile::OnLineFinished(IUiView* pCurrentView, const Vector3d& startPt, const Vector3d& endPt,
    const ISnap* pStartSnap, const ISnap* pEndSnap)
{
    DBG_WARN_AND_RETURN_UNLESS(pCurrentView != nullptr, ElementId::InvalidID, L"当前视图为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_UNLESS(pDoc, ElementId::InvalidID, L"pFamily指针无效",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> ut = IUserTransaction::Create(pDoc, GBMP_TR(L"绘制轮廓"));
    DBG_WARN_AND_RETURN_UNLESS(ut, ElementId::InvalidID, L"ut为空",L"GDMPLab",L"2024-03-30");
    IModelLine* pIModelLine = nullptr;
    {
        pIModelLine = ProfileUsedModelLineUtils::CreateModelLineUsedForProfile(pDoc, ILine3d::Create(startPt, endPt));
        DBG_WARN_AND_RETURN_UNLESS(pIModelLine != nullptr, ElementId::InvalidID, L"创建pLine失败",L"GDMPLab",L"2024-03-30");
        pIModelLine->SetRenderLayer(GraphicsRenderLayer::ProfileCurve);
    }
    bool bCommit = ut->Commit();
    DBG_WARN_AND_RETURN_UNLESS(bCommit, ElementId::InvalidID, L"事务提交失败",L"GDMPLab",L"2024-03-30");

    DBG_WARN_AND_RETURN_UNLESS(pIModelLine->GetOwnerElement(), ElementId::InvalidID, L"pIModelLine->GetOwnerElement()为空",L"GDMPLab",L"2024-03-30");
    return pIModelLine->GetOwnerElement()->GetElementId();
}

//////////////////////////////////////////////////////////////////
//CmdDrawArcProfile

OwnerPtr<IAction> CmdDrawArcProfile::ExecuteCommand(const CommandParameters& cmdParams)
{
    IUiDocument *pUIDoc = UiDocumentViewUtils::GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUIDoc, L"pUIDoc指针无效",L"GDMPLab",L"2024-03-30");
    CmdDrawProfileBase* pCmdProfileBase = static_cast<CmdDrawProfileBase*>(this);
    ActionDrawArcBy3PtsElementInput actionInput;
    actionInput.CommandId = GetCommandId();
    actionInput.DrawArcMode = DrawArcBy3PointsMode::Chained;
    actionInput.ArcFinishedCallback = ActionDrawArcBy3PtsElementInput::CreateOnArcFinishedCallback(&CmdDrawArcProfile::OnLineFinished, this);
    actionInput.EnterGettingEndPointStateCallback = ActionDrawArcBy3PtsElementInput::CreateOnEnterGettingStartPointStateCallback(
        &CmdDrawProfileBase::OnEnterGettingEndPointState, pCmdProfileBase);
    actionInput.ActionDestroyCallback = ActionDrawArcBy3PtsElementInput::CreateOnActionDestroyCallback(&CmdDrawProfileBase::OnActionDestroy, pCmdProfileBase);
    actionInput.PropertyPanelEventCallback = ActionDrawArcBy3PtsElementInput::CreateOnPropertyPanelEventCallback(&CmdDrawProfileBase::OnPropertyPanelEvent, pCmdProfileBase);

    OwnerPtr<IAction> opAction = ActionDrawArcBy3PtsElementCreator::Create(actionInput);

    return TransferOwnership(opAction);
}

ElementId CmdDrawArcProfile::OnLineFinished(IUiView* pCurrentView,
    const Vector3d& startPt, const Vector3d& endPt, const Vector3d& onArcPt,
    const ISnap* pStartSnap, const ISnap* pEndSnap)
{
    DBG_WARN_AND_RETURN_UNLESS(pCurrentView != nullptr, ElementId::InvalidID, L"当前视图为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_UNLESS(pDoc, ElementId::InvalidID, L"pFamily指针无效",L"GDMPLab",L"2024-03-30");

    IModelLine* pModelLine = nullptr;
    OwnerPtr<IUserTransaction> ut = IUserTransaction::Create(pDoc, GBMP_TR(L"绘制轮廓"));
    DBG_WARN_AND_RETURN_UNLESS(ut, ElementId::InvalidID, L"ut为空",L"GDMPLab",L"2024-03-30");
    {
        pModelLine = ProfileUsedModelLineUtils::CreateModelLineUsedForProfile(pDoc, IArc3d::Create(startPt, endPt, onArcPt));
        DBG_WARN_AND_RETURN_UNLESS(pModelLine != nullptr, ElementId::InvalidID, L"创建pLine失败",L"GDMPLab",L"2024-03-30");
    }
    bool bCommit = ut->Commit();
    DBG_WARN_AND_RETURN_UNLESS(bCommit, ElementId::InvalidID, L"事务提交失败",L"GDMPLab",L"2024-03-30");

    DBG_WARN_AND_RETURN_UNLESS(pModelLine->GetOwnerElement(), ElementId::InvalidID, L"pIModelLine->GetOwnerElement()为空",L"GDMPLab",L"2024-03-30");
    return pModelLine->GetOwnerElement()->GetElementId();
}

//////////////////////////////////////////////////////////////////
